﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace EWDT_CDS_Hub.App_Code
{
    public class Timetable
    {
        //properties
        public string StudentId { get; set; }
        public string[,] Subject { get; set; }
        public string[,] Class { get; set; }

        //recommended constructor
        public Timetable(string studId)
        {
            StudentId = studId;
            Subject = new string[6, 12];
            Class = new string[6, 12];
        }

        //Import method
        public bool ImportFromFile(string studId)
        {
            bool status = false;
            string filePath = "Timetables\\" + studId + ".txt";
            if (File.Exists(filePath))
            {
                status = true;
                StreamReader rdr;
                try
                {
                    rdr = new StreamReader(filePath);
                    int dayNo = 0;
                    while (!rdr.EndOfStream)
                    {
                        int periodNo = 0;
                        string tempDayStr = rdr.ReadLine();
                        string[] tempPeriodStr = tempDayStr.Split(';');
                        for (int i = 0; i < tempPeriodStr.Length; i++)
                        {
                            if (tempPeriodStr[i] == "" || tempPeriodStr[i] == "@")
                            {
                                Subject[dayNo, periodNo] = tempPeriodStr[i];
                            }
                            else
                            {
                                string[] tempPeriodItemStr = tempPeriodStr[i].Split(',');
                                Subject[dayNo, periodNo] = tempPeriodItemStr[0];
                                Class[dayNo, periodNo] = tempPeriodItemStr[1];
                            }
                            periodNo++;
                        }
                        dayNo++;
                    }
                    rdr.Close();
                }
                catch
                {
                    status = false;
                }
            }
            return status;
        }

        //Export to a file
        public bool ExportToFile()
        {
            bool status = false;
            string filePath = "Timetables\\" + StudentId + ".txt";

            string tempPath = "Timetables\\Temp_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_ffff") + ".tmp";
            StreamWriter wtr = new StreamWriter(tempPath);
            wtr.Write(ToString());
            wtr.Close();

            string backupPath = filePath.Remove(19) + "_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_ffff") + ".bak";
            File.Replace(tempPath, filePath, backupPath);
            File.Delete(tempPath);
            status = true;
            return status;

        }

        //ToString
        public override string ToString()
        {
            string retStr = "";
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    retStr += Subject[i, j];
                    if (Subject[i, j] != "" && Subject[i, j] != "@" && Subject[i, j] != null)
                    {
                        retStr += "," + Class[i, j];
                    }
                    retStr += ";";
                }
                retStr += "\r\n";
            }
            return retStr;
        }
    }
}